# import pandas as pd
# df = pd.read_csv('project-3-at-2023-04-14-16-42-e279fa08.csv')
# print(df)
import json,sys
path = './project-10-at-2023-04-28-15-00-60258b69.json'
with open(path, 'r', encoding='utf-8') as f:
    row_data = json.load(f)
if (row_data is None):
    sys.exit(1)
print(row_data)
print(type(row_data))
output = []
for dict1 in row_data:
    print(dict1)
    label = dict1['label']
    print(label)
    print(type(label))
    output1 = {}
    output1_text = dict1['text']
    output1['text'] = output1_text
    output1_label = {}
    label_list = []
    label_text_list = []
    for labeldict in label:
        print(labeldict)
        print(type(labeldict))
        start = labeldict['start']
        print(start)
        end = labeldict['end']
        print(end)
        text = labeldict['text']
        print(text)
        labels = labeldict['labels'][0]
        print(labels)
        if labels not in label_list:
            label_list.append(labels)
            dict = {}
            list = []
            listt = []
            listt.append(start)
            listt.append(end)
            list.append(listt)
            dict[text] = list
            output1_label[labels] = dict
            if text not in label_text_list:
                label_text_list.append(text)
        else:
            print(output1_label[labels])
            if text not in label_text_list:
                label_text_list.append(text)
                labels_dict = output1_label[labels]
                list = []
                listt = []
                listt.append(start)
                listt.append(end)
                list.append(listt)
                labels_dict[text] = list
                output1_label[labels] = labels_dict
            else:
                print(output1_label[labels][text])

                labels_dict = output1_label[labels]
                labels_dict_list = labels_dict[text]
                listt = []
                listt.append(start)
                listt.append(end)
                # labels_dict_list.append(listt)
                labels_dict[text] = labels_dict_list
                output1_label[labels] = labels_dict

    output1['label'] = output1_label
    output.append(output1)
    new_output1 = json.dumps(output1, ensure_ascii=False)
    # with open("./test111.json", 'a', encoding='utf-8') as fw:
    #     str = json.dumps(output1, indent=2, ensure_ascii=False)
    #     fw.write(str)
    #     fw.write('\n')

        # json.dump(output1, fw, indent=2, ensure_ascii=False)
        # fw.write('\n')
    file = open('test0428.json', 'a',encoding='utf-8')
    # string = str(new_output1)
    # file.write(string)
    # file.write('\n')
    file.write(new_output1)
    file.write('\n')
    file.close()

new_output = json.dumps(output, ensure_ascii=False)
print(new_output)


# with open('./test0425.json', 'r', encoding='utf-8') as f:
#     for line in f.readlines():
#         json_line = json.loads(line.strip())
#         print(json_line)

print('finish')